<?php 


/**
* 数据链接类
*/
class Model 
{

	public static $pdo;
	public $table;
	public $where='';
	public $orderBy;
	public $filed ="*";
	public $limit;

	
	function __construct($table=null)
	{
		$this->table = $table;
		if($this::$pdo){
			return;
		}else{
			$dsn = "mysql:host=".C('DB_HOST').";dbname=".C('DB_DATABASE');
			$username=C('DB_USER');
			$password =C('DB_PASSWD');

			try{

				$pdo = new Pdo($dsn,$username,$password);

			}catch(PDOException $e) {

				die($e->getMessage());
			}

			$pdo->query("set names ".C('DB_CHARSET'));

			$this::$pdo = $pdo;
		}
		
	}

	public function queryRow($sql=null)
	{
		$result = $this::$pdo->query($sql);

		return $result->fetch(PDO::FETCH_ASSOC);
	}

	public function queryAll($sql=null)
	{
		if($sql==null){
			$sql = "select " .$this->filed ." from ".$this->table." ".$this->where." ".$this->orderBy." ".$this->limit;
		}

		$result = $this::$pdo->query($sql);

		return $result->fetchAll(PDO::FETCH_ASSOC);
		
	}

	public function exec($sql)
	{
		$result = $this::$pdo->exec($sql);

		return $result;
	}

	public function add($arr){

		$keyStr = implode(',', array_keys($arr)) ;
		$valuesStr = implode("','", array_values($arr)) ;

		$sql = "insert into ".$this->table."(".$keyStr.") value('".$valuesStr."')";

		return $this->exec($sql);
	}

	public function where($data)
	{
		$this->where="where ".$data;
		return $this;
	}
	public function orderBy($data)
	{
		$this->orderBy="order by ".$data;
		return $this;
	}

	public function limit($data)
	{
		$this->limit="limit ".$data;
		return $this;
	}

	public function filed($data){
		$this->filed=$data;
		return $this;
	}

}





















 ?>